// components/失物招领/index.js
import {
	dateFormat
} from "../../Utils/Utils"

const app = getApp();

Component({

	options: {
		addGlobalClass: true,
	},

	/**
	 * 组件的属性列表
	 */
	properties: {
		flagId: {
			type: String,
			value: ''
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		// 是否点过此文章的赞
		isPraise: false,
		// 点赞数量
		praise: 0,
		// 评论数量
		comment: 0,
		
		imgClass:"",
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		/**
		 * 根据文章的ID 获取文章的内容
		 * @param {文章id} acticleId
		 */
		loadArticleInfo(acticleId) {
			let that = this;
			wx.cloud.callFunction({
				name: "operation",
				data: {
					function: "getActicleByid",
					_id: acticleId
				}
			}).then(res => {
				let info = res.result.list[0];
				let user = info.user[0];
				that.setData({
					acticleId: info._id, // 文章Id
					image: user.avatarUrl, // 用户头像
					userName: user.nickName, // 用户姓名
					college: user.college, // 用户所在学院

					// 用户发布的图片集合, 最多九张
					pictures: info.pictures,
					time: dateFormat("YYYY-mm-dd HH:MM", new Date(info.timestamp)), // 发布的时间

					isOver: info.isOver, // 是否完结 0/1
					bandonOrpickuUp: info.isChoose ? "拾到东西" : "丢了东西", // 拾取还是捡到
					oldtime: info.loseTime, // 捡到或遗失物品时间
					site: info.loseaddress, // 地点
					type: info.itemType, // 类型
					news: info.text, // 用户发布的内容
					id: user._openid, // 文章作者id
				})
				
				if(info.pictures.length==1) {
					that.setData({
						imgClass:"onlyOne"
					})
				} else if(info.pictures.length==2) {
					that.setData({
						imgClass:"OnlyTwo"
					})
				} else if(info.pictures.length==3) {
					that.setData({
						imgClass:"onlyThree"
					})
				} else {
					that.setData({
						imgClass:"GreaterThanThree"
					})
				}
				
			}).catch(err => {
				console.log(err)
			})
			// 获取是否点赞过此文章
			wx.cloud.callFunction({
					name: "operation",
					data: {
						function: "isPraised",
						articleId: acticleId
					}
				}).then(res => {
					that.setData({
						isPraise: res.result.total>0
					})
				})
				.catch(err => {
					console.log(err)
				})
			// 获取点赞数量
			wx.cloud.callFunction({
					name: "operation",
					data: {
						function: "getPraiseNum",
						articleId: acticleId
					}
				}).then(res => {
					let total = res.result.total
					if (total) {
						that.setData({
							praise: total
						})
					}
				})
				.catch(err => {
					console.log(err)
				})
			// 获取评论数量
			wx.cloud.callFunction({
					name: "operation",
					data: {
						function: "getCommentsNum",
						_id: acticleId
					}
				}).then(res => {
					if (res.result.list.length > 0) {
						that.setData({
							comment: res.result.list[0].count
						})
					}

				})
				.catch(err => {
					console.log(err)
				})
		},
		/**
		 * 点赞模块
		 */
		praise: function () {
			if (!this.data.isPraise) {
				let that = this;
				that.setData({
					praise: this.data.praise + 1,
					isPraise: true,
				})
				wx.cloud.callFunction({
						name: "operation",
						data: {
							function: "praise",
							articleId: this.data.acticleId
						}
					}).then(res => {
						// res.result.stats.updated == 1
						console.log(res)
					})
					.catch(err => {
						console.log(err)
					})
			}
		},

		comment: function () {
			let temp = this.data.flagId
			app.goEssayDetail('detailLoseAndReceive',temp)
		},

		repost: function () {
			const promise = new Promise(resolve => {
				setTimeout(() => {
					resolve({
						title: '自定义转发标题'
					})
				}, 2000)
			})
			return {
				title: '自定义转发标题',
				path: '/page/user?id=123',
				promise
			}
		},

		openPic(e) {
			let {index}=e.currentTarget.dataset
			let pictures = this.data.pictures
			this.triggerEvent("changePic", {
				index,
				pictures: pictures
			})
		},

		change(e) {
		},

		goUserDetail: function () {
			let temp = this.data.id
			app.goUserDetail(temp)
		}
	},

	lifetimes: {
		attached: function () {
			// 在组件实例进入页面节点树时执行
			this.loadArticleInfo(this.data.flagId)
		}
	}
})